<?php

namespace App\Utils\Payment;

use Psr\Http\Message\ResponseInterface;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Pay;
use Yansongda\Supports\Collection;

class DaiFaPayment implements PaymentInterface
{
    private array $config =  [
        'app_id' => '2021004125683064',
        // 必填-应用私钥 字符串或路径
        'app_secret_cert' => 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCpnbC5PUuuBuSHylTy3d8tiqKb6vTpDqUhFaKaNUPhV63vcyA4xy5coeNDr3wZK6YWglMjP0CtM5dOfK3co4Mpt0GS/aKoqmmCPTgQBeucC+eU6oBSDg3x0b5EPdLfdhopKGB7vf3jh57bce3TKI2hZL6wJVx4UPMTem1j6iyJZiaOKg0UDawad2wqrGkCq6JE3M0NnldsAS7rS4xEr5Uwr8jlRefLwoErz8LfN6fE5JDt6J5HowlqALaIOh7pb3OqWn0Qy6Z/0LJ30tIBeww4Mp01BEazMkOYUXVb5LMQwF6pJAWgWCj1En8LKwn6afFMLcgWwG82AC6agUxCJNsrAgMBAAECggEAckj8EHRKj+G5Df24q0Pv4oGs+x2fE4F4pggr3XfSWv13BwYtSdR7WpMBzFNrbJJKgJQjbHZsH8fJRWsgwq+x6uO9XUtk0/yuxsB1C+bRT/19dotjFua9YpiOePCnK0Yqblz0Vaw4a5yv3vCu7D5A3+Q/z/P8kF0Ky68MEexxQcniV/4uJ4o9K674AjQX9gwwrVh6K8FpEfgyL6sM7TwCffIQPpCXsCOMCJ627E+UB8+2UP4vj6bB+Z0g303RMrNeSGG9s2gOTGPl5OuicMa28xmgTvKEzen2kat7oXqnhAQOOVbNmzfY4TT05box+c0ZsE7P+nPAaM2K3RpH4qL1gQKBgQD8tMPU1vn0jqtrRD3GaFkCJ6vgp32+Rnv2yuGPxBIujWn47h/OspJ9AFH8XlI60wyaiMvnlwswTVou3HyYkco7YWc12/i1qvSp80qfGh5Ns59n5AMeSEcfWhx6JH0V0lND0rbcDjlPMaJEGJ5FIZuDntnkNaaYsMYCaYPLV/vWIQKBgQCr06sgVPW6mI/XGx42kuyoPp+k8HRyTs1HklM1+c8h3qVl8Ij0BpsL1UHJdEc5oUX49wRuWeu0DZIqs6rI55otDRYqjJMFTiCsI0Q+78vqgmRSlvxKHvruSIOtxQhejxHsKSYaurp5eYXghCZvgn0s737ziVSVXc4xXtc8OOIvywKBgA640TfTFljC/wWOwjKO3bCccwzxdQfiv1IPW7iuaYjAwE26QawfBx21KRGUxSGcLf3KvmZNjF9SAlD8k12oEKoI8KRYnlKnFP3ZqJ1APke0kTLSO8RF3a7b5xDOk2L0D9nBqtmDEp+kepmQ/Zd1b8PpOzRqU7xNgBuf3ge4pnlBAoGAQ6gDsKZjX0alFJDD878kWhbWa26Wm0+1+qF+DDnBulddjucINfXH5P/ieH1FckazM5Wn8/+KFIpnbFL0Q0kWZvjJKpXRafxaVw1H2PQ4uaz/7Zkv28uZCGwbFTgI1JrG9tM5ArtVo3EbKs0+CRg7S4890IzZdjrKbpwmabsILMMCgYAYHonneAVjR7K4TqIW8K2oNCE7yywMVeA2UJEQNzgtMYvzfNrpVqYAGvK1uwAyN8+RQwDc8la4st6VL6BHCIA2pPRCSA39T2sHusFt+ryc/FGbey7fzbXFHduZZMyZMs1b+g9Zav+IWab6ypRlhB0ZTgXGB1nAnYXGF+tBjsnKAA==',
        // 必填-应用公钥证书 路径
        'app_public_cert_path' => '/www/wwwroot/default/hb/public/alipay/appPublicCert.crt',
        // 必填-支付宝公钥证书 路径
        'alipay_public_cert_path' => '/www/wwwroot/default/hb/public/alipay/alipayPublicCert.crt',
        // 必填-支付宝根证书 路径
        'alipay_root_cert_path' => '/www/wwwroot/default/hb/public/alipay/alipayRootCert.crt',
        'return_url' => 'https://yansongda.cn/alipay/return',
        'notify_url' => 'https://yansongda.cn/alipay/notify',
        // 选填-默认为正常模式。可选为： MODE_NORMAL, MODE_SANDBOX, MODE_SERVICE
        'mode' => Pay::MODE_NORMAL,
    ];

    /**
     * @param string $title
     * @param string $order_no
     * @param string $money
     * @return ResponseInterface
     * @throws ContainerException
     */
    public function appPay(string $title, string $order_no, string $money): ResponseInterface
    {
        Pay::config($this->getConfig());
        return Pay::alipay()->app([
            'out_trade_no'  => $order_no,
            'total_amount'  => $money,
            'subject'       => $title,
        ]);
    }

    /**
     * @param string $title
     * @param string $order_no
     * @param string $money
     * @return ResponseInterface
     * @throws ContainerException
     */
    public function wapPay(string $title, string $order_no, string $money): ResponseInterface
    {
        Pay::config($this->getConfig());
        return Pay::alipay()->wap([
            'out_trade_no'  => $order_no,
            'total_amount'  => $money,
            'subject'       => $title,
            'quit_url'      => 'https://yansongda.cn',
        ]);
    }

    /**
     * @param string $title
     * @param string $order_no
     * @param string $money
     * @return ResponseInterface
     * @throws ContainerException
     */
    public function webPay(string $title, string $order_no, string $money): ResponseInterface
    {
        Pay::config($this->getConfig());
        return Pay::alipay()->web([
            'out_trade_no'  => $order_no,
            'total_amount'  => $money,
            'subject'       => $title,
        ]);
    }

    /**
     * @param string $order_no
     * @return array|Collection
     * @throws ContainerException
     * @throws InvalidParamsException
     * @throws ServiceNotFoundException
     */
    public function payQuery(string $order_no): Collection|array
    {
        Pay::config($this->config);
        return Pay::alipay()->find([ 'out_trade_no' => $order_no ]);
    }

    public function notify()
    {
        // TODO: Implement notify() method.
    }

    public function getConfig(): array
    {
        return [
            'alipay' => [
                'default' => $this->config
            ],
            'logger' => [
                'enable' => false,
                'file' => './logs/pay.log',
                'level' => 'debug', // 建议生产环境等级调整为 info，开发环境为 debug
                'type' => 'daily', // optional, 可选 daily.
                'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
            ],
            'http' => [ // optional
                'timeout' => 5.0,
                'connect_timeout' => 5.0,
            ],
        ];
    }
}
